<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 过敏史章节 -->
	<xsl:template match="*" mode="AllergiesSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_AllergiesAdverseReactions]" mode="AllergiesEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 过敏史章节条目 -->
	<xsl:template match="*" mode="AllergiesEntry">
		<xsl:choose>
		    <!-- C0018,C0019,C0032,C0033 -->
			<xsl:when test="($hccDocType='C0018' or $hccDocType='C0019' or $hccDocType='C0032' or $hccDocType='C0033') and (hl7:entry/hl7:act/hl7:entryRelationship/hl7:observation/hl7:code/@code='DE02.10.023.00' and hl7:entry/hl7:act/hl7:entryRelationship/hl7:observation/hl7:value/@value='true')">
				<xsl:apply-templates select="hl7:entry/hl7:act/hl7:entryRelationship" mode="HCCAllergy">
					<xsl:with-param name="sectionType" select="'ALL'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- C0002,C0003 -->
			<xsl:when test="$hccDocType='C0002' or $hccDocType='C0003'">
				<xsl:apply-templates select="hl7:entry" mode="HCCAllergy">
					<xsl:with-param name="sectionType" select="'ALL'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- C0001,C0017,C0023 -->
			<xsl:otherwise>
				<xsl:apply-templates select="hl7:entry" mode="HCCAllergy">
					<xsl:with-param name="sectionType" select="'ALL'"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	
	
	<!-- 既往史章节 -->
	<xsl:template match="*" mode="AllergiesOfPastIllnessSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfPastIllness]" mode="AllergiesOfPastIllnessEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 既往史章节条目 -->
	<xsl:template match="*" mode="AllergiesOfPastIllnessEntry">
		<xsl:choose>
		    <!-- C0010,C0034,C0046 -->
			<xsl:when test="$hccDocType='C0010' or $hccDocType='C0034' or $hccDocType='C0046'">	    
				<xsl:apply-templates select="hl7:entry[hl7:observation/hl7:code/@code='DE02.10.022.00' ]" mode="HCCAllergy">
					<xsl:with-param name="sectionType" select="'API'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- C0008 -->
			<xsl:when test="$hccDocType='C0008'">	    
				<xsl:apply-templates select="hl7:entry[hl7:observation/hl7:code/@code='DE02.10.023.00' and hl7:observation/hl7:value/@value='true']" mode="HCCAllergy">
					<xsl:with-param name="sectionType" select="'API'"/>
				</xsl:apply-templates>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	
	<!--通用过敏条目输出模板-->
	<xsl:template match="*" mode="HCCAllergy">
		<xsl:param name = "sectionType" />
		<Allergy>
			<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
			<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
			<xsl:choose>
				<xsl:when test="$hccDocType='C0002' or $hccDocType='C0003'">
					<FreeTextAllergy><xsl:value-of select="hl7:observation/hl7:entryRelationship/hl7:observation/hl7:value"/></FreeTextAllergy>
				</xsl:when>
				<xsl:when test="$hccDocType='C0001' or $hccDocType='C0017' or $hccDocType='C0023'">
					<FreeTextAllergy><xsl:value-of select="hl7:observation/hl7:value"/></FreeTextAllergy>
				</xsl:when>
				<xsl:when test="$hccDocType='C0018' or $hccDocType='C0019' or $hccDocType='C0032' or $hccDocType='C0033'">
					<AllergyCategory>
						<Code><xsl:value-of select="hl7:observation/hl7:participant/hl7:participantRole/hl7:playingEntity/hl7:code/@code"/></Code>
						<Description><xsl:value-of select="hl7:observation/hl7:participant/hl7:participantRole/hl7:playingEntity/hl7:code/@displayName"/></Description>
					</AllergyCategory>
					<Allergy>
					<Code><xsl:value-of select="hl7:observation/hl7:participant/hl7:participantRole/hl7:playingEntity/hl7:desc"/></Code>
					<Description><xsl:value-of select="hl7:observation/hl7:participant/hl7:participantRole/hl7:playingEntity/hl7:desc"/></Description>
					</Allergy>
				</xsl:when>
				<xsl:when test="$hccDocType='C0010' or $hccDocType='C0034' or $hccDocType='C0046'">
					<FreeTextAllergy><xsl:value-of select="hl7:observation/hl7:value"/></FreeTextAllergy>
				</xsl:when>
				<xsl:when test="$hccDocType='C0008'">
					<FreeTextAllergy><xsl:value-of select="hl7:observation/hl7:entryRelationship/hl7:observation/hl7:value"/></FreeTextAllergy>
				</xsl:when>
			</xsl:choose>
		</Allergy>		
	</xsl:template>
	
</xsl:stylesheet>
